package com.wangdong.mvpretrofitokhttprxjava.view;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.wangdong.mvpretrofitokhttprxjava.R;
import com.wangdong.mvpretrofitokhttprxjava.base.BaseActivity;
import com.wangdong.mvpretrofitokhttprxjava.common.AppStatusConstant;
import com.wangdong.mvpretrofitokhttprxjava.common.BaseContent;
import com.wangdong.mvpretrofitokhttprxjava.model.BaseModel;
import com.wangdong.mvpretrofitokhttprxjava.model.bean.MainBean;
import com.wangdong.mvpretrofitokhttprxjava.model.bean.TextBean;
import com.wangdong.mvpretrofitokhttprxjava.presenter.MainPresenter;
import com.wangdong.mvpretrofitokhttprxjava.utils.L;
import com.wangdong.mvpretrofitokhttprxjava.utils.RetrofitUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MainActivity extends BaseActivity<MainPresenter> implements MainView, View.OnClickListener {


    @BindView(R.id.back_name)
    TextView backName;
    @BindView(R.id.back)
    LinearLayout back;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_right)
    TextView tvRight;
    @BindView(R.id.rlt_base)
    RelativeLayout rltBase;
    @BindView(R.id.btn_first)
    Button btnFirst;
    @BindView(R.id.btn_second)
    Button btnSecond;
    @BindView(R.id.btn_second_2)
    Button btnSecond2;
    @BindView(R.id.btn_third)
    Button btnThird;
    @BindView(R.id.btn_forth)
    Button btnForth;
    @BindView(R.id.btn_fifth)
    Button btnFifth;
    @BindView(R.id.btn_six)
    Button btnSix;
    @BindView(R.id.btn_down)
    Button btnDown;
    @BindView(R.id.tv_text)
    TextView tvText;

    @Override
    protected MainPresenter createPresenter() {
        return new MainPresenter(this);
    }
    @Override
    protected int getLayoutId() {
        return R.layout.activity_main;
    }
    @Override
    protected void initData() {
    }

    @Override
    protected String getContentTitle() {
        return "王东开头";
    }


    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        int action = intent.getIntExtra(AppStatusConstant.KEY_HOME_ACTION, AppStatusConstant.ACTION_BACK_TO_HOME);
        switch (action) {
            case AppStatusConstant.ACTION_RESTART_APP:
                protectApp();
                break;
        }
    }
    @Override
    protected void protectApp() {
        Toast.makeText(getApplicationContext(), "应用被回收重新启动", Toast.LENGTH_LONG).show();
        startActivity(new Intent(this, SplashActivity.class));
        finish();
    }

    @Override
    public void onClick(View v) {
    }
    @Override
    public void onMainSuccess(BaseModel<List<MainBean>> o) {
        //        Log.e(o.getErrmsg(), "");
//        Log.e(o.getErrcode() + "", "");
//        mainBeans.addAll(o.getData());
//        Log.e("sm", mainBeans.toString() + "");
//        mTvText.setText(o.getData().toString());
    }

    @Override
    public void onTextSuccess(BaseModel<TextBean> o) {
        tvText.setText(o.getResult().getData().toString());
    }
    /**
     * 上传图片成功
     *
     * @param o
     */
    @Override
    public void onUpLoadImgSuccess(BaseModel<Object> o) {
        L.e("文件视频路径==" + o.getResult());
    }
    /**
     * 加载列表成功
     *
     * @param o
     */
    @Override
    public void onTableListSuccess(BaseModel<Object> o) {
        L.e("测试多基类1=" + o.getResult());
    }

    @Override
    public void onRestrictionsSuccess(BaseModel<Object> o) {
        L.e("测试多基类2=" + o.getResult());
    }

    @Override
    public void onCheShiSuccess(BaseModel<Object> o) {
        L.e("测试多基类3=" + o.getResult());
    }
    @Override
    public void onErrorCode(BaseModel model) {
        //super.onErrorCode(model);
        //我要自定义
        //实现自己的逻辑
    }
    @Override
    public void showError(String msg) {
        if (msg.contains("重新登录")) {
        //处理自己定制逻辑
        } else {
            super.showError(msg);
        }

    }
    @OnClick({R.id.back_name, R.id.back, R.id.tv_title, R.id.tv_right, R.id.rlt_base, R.id.btn_first, R.id.btn_second, R.id.btn_second_2, R.id.btn_third, R.id.btn_forth, R.id.btn_fifth, R.id.btn_six, R.id.btn_down, R.id.tv_text})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back_name:
                break;
            case R.id.back:
                break;
            case R.id.tv_title:
                break;
            case R.id.tv_right:
                break;
            case R.id.rlt_base:
                break;
            //默认请求
            case R.id.btn_first:
                mPresenter.getTextApi();
                break;
            //上传图片
            case R.id.btn_second:
                showToast("添加本地图片url,再进行测试");
//                upLoadImage();
                break;
            //上传文件进度测试
            case R.id.btn_second_2:
                mPresenter.upLoadVideoApi(BaseContent.baseFileName+"ceshi.mp4");
                break;
            //mvc测试
            case R.id.btn_third:
//                startActivity(MvcActivity.class);
                break;
            //多基类测试
            case R.id.btn_forth:
                mPresenter.getTableListApi();
                break;
            //多基类测试
            case R.id.btn_fifth:
                mPresenter.getRestrictionsApi();
                break;
                //多基本类测试
            case R.id.btn_six:
                mPresenter.getCheShiApi();
                break;
            case R.id.btn_down:
                //文件上传
//                startActivity(FileActivity.class);
                break;
            case R.id.tv_text:
                break;
            default: break;
        }
    }

    private void upLoadImage() {
        /**
         * 俩个参数  一个是图片集合路径   一个是和后台约定的Key，如果后台不需要，随便写都行
         */
        List<String> strings = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            strings.add("tupian.lujing");
        }
        mPresenter.upLoadImgApi(
                "title",
                "content",
                RetrofitUtil.filesToMultipartBodyParts(RetrofitUtil.initImages(strings), "tupian.key"));

    }
}
